package com.hippy.erya.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.common.DateUtil;
import com.hippy.erya.entity.RawMaterialDailyLossDO;
import com.hippy.erya.entity.RawMaterialDailyLossEntity;
import com.hippy.erya.mapper.RawMaterialDailyLossMapper;
import com.hippy.erya.query.BaseQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Service
public class RawMaterialDailyLossServiceImpl extends ServiceImpl<RawMaterialDailyLossMapper, RawMaterialDailyLossDO> {

    @Autowired
    private RawMaterialDailyLossMapper rawMaterialDailyLossMapper;

    @Autowired
    private RawMaterialDictionaryServiceImpl rawMaterialDictionaryService;


    public List<RawMaterialDailyLossEntity> queryLastWeek() {
        QueryWrapper<RawMaterialDailyLossDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.ge("CREATED_AT", DateUtil.plusDays(DateUtil.getCurrentDate(), -7));
        List<RawMaterialDailyLossDO> lossDOList = rawMaterialDailyLossMapper.selectList(queryWrapper);
        return convert(lossDOList);
    }

    private List<RawMaterialDailyLossEntity> convert(List<RawMaterialDailyLossDO> lossDOList) {
        if (CollectionUtils.isEmpty(lossDOList)) {
            return null;
        }
        Map<String, Double> codeMap = new HashMap<>();
        lossDOList.forEach(lossDo -> {
            codeMap.put(lossDo.getRawMaterialCode(), codeMap.getOrDefault(lossDo.getRawMaterialCode(), 0.0) + lossDo.getRawMaterialLossNum());
        });
        Map<String, String> map = rawMaterialDictionaryService.codeToName(String.valueOf(LoginContextHolder.getContext().getUser().getId()));
        List<RawMaterialDailyLossEntity> rawMaterialDailyLossEntityList = new ArrayList<>();
        codeMap.forEach((k, v) -> {
            RawMaterialDailyLossEntity rawMaterialDailyLossEntity = new RawMaterialDailyLossEntity();
            rawMaterialDailyLossEntity.setName(map.getOrDefault(k, k));
            rawMaterialDailyLossEntity.setQuantity(v);
            rawMaterialDailyLossEntityList.add(rawMaterialDailyLossEntity);
        });
        rawMaterialDailyLossEntityList.sort(Comparator.comparingDouble(RawMaterialDailyLossEntity::getQuantity));
        if (rawMaterialDailyLossEntityList.size() > 5) {
            return rawMaterialDailyLossEntityList.subList(0, 5);
        }
        return rawMaterialDailyLossEntityList;
    }


    public Page<RawMaterialDailyLossDO> queryByPage(Long currentPage, int limit, String materialCode,
                                                    String businessDate) {
        Page<RawMaterialDailyLossDO> page = new Page<>(currentPage, limit);
        QueryWrapper<RawMaterialDailyLossDO> queryWrapper = BaseQueryWrapper.buildQuery();
        if (StringUtils.isNotEmpty(materialCode)) {
            queryWrapper.eq("RAW_MATERIAL_CODE", materialCode);
        }
        BaseQueryWrapper.buildQuery(queryWrapper, businessDate, "BUSINESS_DATE");
        Page<RawMaterialDailyLossDO> pageResult = rawMaterialDailyLossMapper.selectPage(page, queryWrapper);
        Map<String, String> map = rawMaterialDictionaryService.codeToName(null);
        pageResult.getRecords().forEach(rawMaterialLossDO -> {
            BaseQueryWrapper.outputMessage(rawMaterialLossDO);
            rawMaterialLossDO.setRawMaterialName(map.get(rawMaterialLossDO.getRawMaterialCode()));
        });
        return pageResult;
    }
}
